package jarvis.fs;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.store.Directory;

/**
 * 
 * Function: The Document Frequency measure
 * @author Jarvis.Guo
 *
 */
public class DF extends FeatureSelection {

	public DF(Directory dir)
	{
		super(dir);
	}
	
	
	/* (non-Javadoc)
	 * @see jarvis.fs.FeatureSelection#getTopNFeature(int)
	 */
	@Override
	public List<ComparableTerm> getFeatures() {
		
		List<ComparableTerm> list = new ArrayList<ComparableTerm>();
		try{
			TermEnum te = indexReader.terms();
			TermDocs td = null;
			
			while(te.next())
			{
				Term term = te.term();
				int value1 = te.docFreq();//doc frequence
				td = indexReader.termDocs(term);
				int[] docs=new int[value1],freqs = new int[value1];
				//read the document number and frequence in them where the term appears
				td.read(docs, freqs);
				int value2 = 0;//the all occurrence
				for(int freq:freqs)//frequence in all documents
				{
					value2 += freq;
				}
				list.add(new TwoIntComparableTerm(term,value1,value2));
			}
		}
		catch(IOException ex)
		{
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
		
		return list;
	}

	

}
